<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
          :props="{children:'children',label:'name'}"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children &&data.children.length ===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title="isEdit?'编辑部门':'新增部门'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false" @close="resetForm">
        <AddorEdit :id="curId" ref="addOrEditREF" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="hClose" /></el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartmentList, delDepartments } from '@/api/departments'
import { tranDataToTree } from '@/utils'
import AddorEdit from './depDIalog.vue'
export default {
  name: 'Departments',
  components: { AddorEdit },
  data() {
    return {
      isEdit: false,
      showDialog: false, // 控制弹窗的显示与隐藏
      // 依赖一份树形数据
      list: [],
      curId: '', // 保存当前部门id
      originList: [] // 保存子组件需要的信息
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.addOrEditREF.resetFields()
    },
    // 子组件取消按钮关闭弹窗
    hClose() {
      this.showDialog = false // 控制弹窗的显示与隐藏
    },
    // 关闭弹窗重新渲染
    hSuccess() {
      this.loadDepartmentList()
      this.showDialog = false
    },
    // 删除部门
    hDel(id) {
      this.$confirm('是否确定删除该部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartments(id)
        // console.log('hDel', res)
        this.hSuccess()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击添加打开弹窗
    hAdd(id) {
      this.showDialog = true
      this.curId = id // 获取部门id 保存到data函数中
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id // 获取部门id 保存到data函数中
      this.isEdit = true
      // dom的更新是异步的 this.$nextTick 解决会解决异步更新
      this.$nextTick(() => {
        // 解决数据不更新的问题 this.$refs.addOrEditREF 可以获取子组件,可以直接操作子组件的一切
        this.$refs.addOrEditREF.loadDepartDetail()
      })
    },
    // 获取部门列表
    async loadDepartmentList() {
      try {
        const { data: res } = await getDepartmentList()
        // console.log('getDepartmentList', res)
        res.depts.shift() // 删除第一条没有用的数据
        this.list = tranDataToTree(res.depts)
        this.originList = res.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // 保存数据
      } catch (err) {
        console.log('getDepartmentList', err)
      }
    }
  }
}
</script>
